/*
 * ------------------------------------------------------------------
 *  Copyright © 2018. Hangzhou DtDream Technology Co.,Lt d. All rights reserved.
 *  ------------------------------------------------------------------
 *        Product: order-center
 *    Module Name: order-api
 *   Date Created: 18-1-16 下午12:52
 *    Description:
 *  ------------------------------------------------------------------
 *  Modification History
 *  DATE            Name           Description
 *  18-1-16 下午12:52 汪志意
 *  ------------------------------------------------------------------
 */

package com.example.demo.util;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.google.common.base.Throwables;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cyx
 * @date 2018/1/9 11:15
 */
@Slf4j
public class JsonUtil {

    public final static ObjectMapper mapper;//Json.NON_EMPTY;

    static {
        mapper = new ObjectMapper();
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        mapper.registerModule(simpleModule);

        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        mapper.disable(SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS);
        mapper.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
    }

    public static String toJsonStr(Object object) {
        String result = "";
        if (object == null) {
            return result;
        }
        try {
            result = mapper.writeValueAsString(object);
        } catch (Exception var3) {
            log.error("fail.object.to.string[]cause:{}", Throwables.getStackTraceAsString(var3));
        }
        return result;
    }

    public static <T> T toJavaBean(String jsonStr, Class<T> valueType) {
        if (StringUtils.isBlank(jsonStr)) {
            return null;
        }
        try {
            return mapper.readValue(jsonStr, valueType);
        } catch (Exception var3) {
            log.error("fail.String.to.object[]cause:{}", Throwables.getStackTraceAsString(var3));
        }
        return null;
    }

    public static  <T> List<T>  toJavaBeanList(String jsonStr, Class<T> valueType) {
        if (StringUtils.isBlank(jsonStr)) {
            return null;
        }
        try {
            CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, valueType);
            return mapper.readValue(jsonStr, listType);
        } catch (Exception var3) {
            log.error("fail.String.to.object[]cause:{}", Throwables.getStackTraceAsString(var3));
        }
        return null;
    }


    public static Map<String, Object> toMap(String json) {
        Map<String, Object> result = new HashMap<>();
        try {
            result = mapper.readValue(json, new TypeReference<Map<String, Object>>() {
            });
        } catch (Exception var3) {
            log.error("fail.object.to.string[]cause:{}", Throwables.getStackTraceAsString(var3));
        }
        return result;
    }

    public static List<Map<String, Object>> toListMap(String json) {
        List<Map<String, Object>> result = new ArrayList<>();
        try {
            result = mapper.readValue(json, new TypeReference<List<Map<String, Object>>>() {
            });
        } catch (Exception var3) {
            log.error("fail.object.to.string[]cause:{}", Throwables.getStackTraceAsString(var3));
        }
        return result;
    }


}
